package com.example.cashier

import android.graphics.Bitmap
import android.os.Bundle
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.example.cashier.databinding.ActivityCodeBinding
import com.example.cashier.entity.CashierAddParam
import com.example.cashier.entity.CashierEntityItem
import com.example.cashier.vm.CashierViewModel
import com.example.common.view.view.BaseMvvmActivity
import com.google.zxing.BarcodeFormat
import com.google.zxing.MultiFormatWriter
import com.google.zxing.common.BitMatrix
import com.journeyapps.barcodescanner.BarcodeEncoder
import dagger.hilt.android.AndroidEntryPoint
import java.text.SimpleDateFormat

@AndroidEntryPoint
@Route(path ="/cashier/CodeActivity")
class CodeActivity : BaseMvvmActivity<ActivityCodeBinding>() {
    override fun getLayoutId(): Int =R.layout.activity_code
    @Autowired
    lateinit var payer: CashierEntityItem
    val cashierViewModel:CashierViewModel by viewModels ()
    override fun initView() {
        super.initView()
        ARouter.getInstance().inject(this)
        mDataBinding.ivBack.setOnClickListener {
            finish()
        }
        mDataBinding.tvAddress.text= SPUtils.getInstance().getString("gaode").toString()
        mDataBinding.tvName.text = payer.payerName
        mDataBinding.payerMoney.text = "￥${payer.actualAmount}"
        payer.effectiveDate = SimpleDateFormat("yyyy-MM-dd").format(System.currentTimeMillis())
        payer.expirationDate = SimpleDateFormat("2026-MM-dd").format(System.currentTimeMillis())
        payer.paymentStatus = "已缴费"
        mDataBinding.over.setImageBitmap(generateQRCode("支付成功"))
        mDataBinding.btnSend.setOnClickListener {
            var cashierAddParam=CashierAddParam(
                actualAmount = payer.actualAmount,
                chargeAmount = payer.chargeAmount,
                receivableAmount = payer.receivableAmount,
                chargeType = payer.chargeType,
                payerName = payer.payerName,
                contactNumber = payer.contactNumber.toLong(),
                balanceDeduction = payer.balanceDeduction,
                discountAmount = payer.discountAmount,
                effectiveDate = payer.effectiveDate,
                expirationDate = payer.expirationDate,
                id = payer.id,
                operator = payer.operator,
                payerId = payer.payerId,
                paymentMethod = payer.paymentMethod,
                paymentStatus = "已缴费",
                paymentTime = payer.paymentTime,
                penalty = payer.penalty,
            )
            cashierViewModel.updateCashier(payer.id,cashierAddParam)
        }
        cashierViewModel.mSuccessLiveData.observe(this){
            ToastUtils.showLong("缴费成功")
            ARouter.getInstance().build("/cashier/CodeXiangActivity")
                .withSerializable("payer2",payer)
                .navigation()
        }
    }
    //生成二维码
    private fun generateQRCode(context: String): Bitmap? {
        val bitMatrix: BitMatrix = MultiFormatWriter().encode(
            context, BarcodeFormat.QR_CODE,150,150)
        val barcodeEncoder = BarcodeEncoder().createBitmap(bitMatrix)
        return barcodeEncoder
    }
}